package com.hst.capacity.application.biz.impl;

import com.hst.capacity.application.biz.FranchiseeBiz;
import com.hst.capacity.application.biz.InvoiceBiz;
import com.hst.capacity.application.mail.MailService;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.invoice.InvoiceEntity;
import com.hst.capacity.domain.model.request.invoice.InvoiceLstParam;
import com.hst.capacity.domain.model.request.invoice.InvoiceParam;
import com.hst.capacity.domain.model.response.PagingResultVO;
import com.hst.capacity.domain.model.response.invoice.InvoiceVO;
import com.hst.capacity.domain.service.InvoiceService;
import com.hst.capacity.infra.persistence.jpa.invoice.InvoiceRepository;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InvoiceBizImpl implements InvoiceBiz {

    @Autowired
    InvoiceRepository invoiceRepository;

    @Autowired
    InvoiceService invoiceService;

    @Autowired
    FranchiseeBiz franchiseeBiz;

    @Autowired
    MailService mailService;

    @Override
    public PagingResultVO<InvoiceVO> findInvoiceLst(InvoiceLstParam param) {
        String idFranchiseeArr = franchiseeBiz.findIdFranArrByFinance(param.getIdFinance());
        if (StringUtil.isNotBlank(idFranchiseeArr)) {
            param.setIdFranchiseeArr(idFranchiseeArr);
            param.setIdFranchisee(null);
        }
        return invoiceService.searchWithConditionsAndPaging(param);
    }

    @Override
    public InvoiceVO findByIdInvoice(String idInvoice) {
        return invoiceService.findByIdInvoice(idInvoice);
    }

    @Override
    public InvoiceVO findByIdOrder(String idOrder) {
        return invoiceService.findByIdOrder(idOrder);
    }

    @Override
    public InvoiceVO findByExternalOrderId(String idExternalOrder) {
        return invoiceService.findByExternalOrderId(idExternalOrder);
    }

    @Override
    public String updInvoiceInfo(InvoiceParam param) {
        return invoiceService.updInvoiceInfo(param);
    }

    @Override
    public String invalidInvoice(InvoiceParam param) {
        return invoiceService.invalidInvoice(param);
    }

    @Override
    public List<InvoiceVO> findAllInvoiceLst(InvoiceLstParam param) {
        return invoiceService.findAllInvoiceLst(param);
    }

    @Override
    public void saveInvoicePdf(String idInvoice, String pdfPath) {
        invoiceService.updInvoicePdfUrl(idInvoice, pdfPath);
    }

    @Override
    public boolean sendMail(InvoiceParam param) {
        InvoiceEntity entity = invoiceRepository.findByIdInvoice(param.getIdInvoice());
        if (!(entity.getInvoiceStatus() == 2 ||
                entity.getInvoiceStatus() == 3))
            throw new BusinessException(5001,"发票状态没有开具，没有发票信息无法发送邮件。");
        String from = "23201068@qq.com";
        String to = entity.getInvoiceSendEmail();
        if (StringUtil.isBlank(to))
            throw new BusinessException(5001,"收件人邮件不能为空");
        String subject = "发票-订单号：" + entity.getExternalOrderId();
        String pdfUrl = "https://image.amading.com/" + entity.getInvoiceUrl();
        //String text = "<h1>尊敬的用户，您好：<br>您的发票已发送，请查收附件。<br>点击链接下载。链接地址为：" + pdfUrl;
        String text = "<h1>尊敬的用户，您好：</h1><br>&nbsp;&nbsp;您的发票已发送，请查收附件。" +
                "<br>发票抬头："+ entity.getInvoiceTitle() +
                "<br>发票金额：" + entity.getInvoiceAmount() / 100 + "元" +
                "<br>发票链接：" + pdfUrl;
        String[] attachmentURL = {pdfUrl};
        mailService.sendMail(from, to, subject, text, attachmentURL);

        entity.setInvoiceStatus(Constants.InvoiceStatus.SEND_DONE);
        invoiceRepository.save(entity);
        return true;
    }

    @Override
    public boolean sendMail(String idInvoice) {
        InvoiceEntity entity = invoiceRepository.findByIdInvoice(idInvoice);
        if (!(entity.getInvoiceStatus() == 2 ||
                entity.getInvoiceStatus() == 3))
            throw new BusinessException(5001,"发票状态没有开具，没有发票信息无法发送邮件。");
        String from = "23201068@qq.com";
        String to = entity.getInvoiceSendEmail();
        if (StringUtil.isBlank(to))
            throw new BusinessException(5001,"收件人邮件不能为空");
        String subject = "发票-订单号：" + entity.getExternalOrderId();
        String pdfUrl = "https://image.amading.com/" + entity.getInvoiceUrl();
        //String text = "<h1>尊敬的用户，您好：<br>您的发票已发送，请查收附件。<br>点击链接下载。链接地址为：" + pdfUrl;
        String text = "<h1>尊敬的用户，您好：</h1><br>&nbsp;&nbsp;您的发票已发送，请查收附件。" +
                "<br>发票抬头："+ entity.getInvoiceTitle() +
                "<br>发票金额：" + entity.getInvoiceAmount() / 100 + "元" +
                "<br>发票链接：" + pdfUrl;
        String[] attachmentURL = {pdfUrl};
        mailService.sendMail(from, to, subject, text, attachmentURL);

        entity.setInvoiceStatus(Constants.InvoiceStatus.SEND_DONE);
        invoiceRepository.save(entity);
        return true;
    }

}